import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {NativeProvider} from "@providers/natives/native";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {CustomerProvider} from "@providers/interface/customer";
import {ImageManipulationProvider} from "@providers/interface/image-manipulation";
import {ImagePickerObject} from "@common/ImagePickerObject";

/**
 * Generated class for the CommentPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-comment',
  templateUrl: 'comment.html',
})
export class CommentPage {

  data: any = {};
  userInfo: any = this.storage.read("userInfo");

  picList: any[] = []; //手机选择的图片

  comment: any = {
    "commentType": "",
    "grade": {"BBXF": 5, "WLFW": 5, "FWTD": 5},
    "text": "",
    "isAnonymity": true,
    "isCharge": false,
    "ID": this.data["ID"]
  };
  DJLX: any = this.data["DJLX"];

  starLXs: any[] = [];//评分类型

  isApp;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private native: NativeProvider,
              private storage: StorageProvider,
              private viewCtrl: ViewController,
              private dialog: DialogProvider,
              private customer: CustomerProvider,
              private imageMpn:ImageManipulationProvider) {
    this.data = this.navParams.data;
    this.isApp = this.native.isCordova;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CommentPage');
    this.initStarLX();
    this.imagedowablelode();
  }

  initStarLX() {
    this.customer
      .getEvaluationType({"ID": this.data.ID})
      .then((message) => {
        this.starLXs = message.data;
        for (let i=0;i<this.starLXs.length;i++){
          let value = this.starLXs[i];
          value["GRADE"] = {
            star: 5,
            starMap: [
              '不满意',
              '还行',
              '一般',
              '满意',
              '很满意',
            ]
          };
        }
      }).catch( (error) => {
        this.dialog.showAlert("请检查网络！"+error);
      });
  }

  dismiss(data?: any) {
    this.viewCtrl.dismiss(data);
  }

  onSubmit() {
    this.dialog.showLoading();
    if (this.isApp) {
      this.comment.commentType = 'mobile';
    } else {
      this.comment.commentType = 'wechat';
    }
    this.comment.grade = this.starLXs;
    this.comment.partyid = this.userInfo.PARTYID;
    this.comment.pictures = this.picList.join(',');
    this.comment.ID = this.data['ID'];
    console.log(JSON.stringify(this.comment));
    this.customer.subComment(this.comment).then(() => {
      this.dialog.hideLoading();
      this.dialog.showAlert("发布成功！");
      this.dismiss(true);
    }).catch(() => {
      this.dialog.hideLoading();
      this.dialog.showAlert("发布失败！");
    });
  }

  imagedowablelode() {
    this.imageMpn.imagedowablelode({ID: this.data.ID}).then(data => {
      let datas: any[] = data['data'];
      let files: ImagePickerObject[] = [];
      for (let i = 0; i < datas.length; i++) {
        let file = new ImagePickerObject();
        file.url = datas[i]['PICTURE'];
        file.id = datas[i]['XLH'];
        files.push(file);
      }
      this.picList=files
    }).catch(() => {
    });
  }

  delImage(event) {
    this.imageMpn.delImage({XLH:event[0].id}).then(() => {
      this.imagedowablelode()
    }).catch(() => {
    });
  }

  imageUpload(event) {
    this.imageMpn.imageUpload({
      ID: this.data.ID,
      PICTURE:event[0].FILE_URL
    }).then((data) => {
      event[0].id = data.message;
      console.log(event)
    }).catch(() => {
      /*this.dialog.showAlert(JSON.stringify(error))*/
    });
  }

}
